package com.jeecg.study.common.utils;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

/**
 * @see 解析xml专用 
 * @author wuhn
 * @date
 */
public class XmlUtils {

	private	static SAXBuilder builder=new SAXBuilder();
	
	private static Document document =null;
	
	private static Map<String,String> parseXML(){
		Map<String,String> map= new HashMap<>();
		Element rootNode = document.getRootElement();
		List<Element> actionList = rootNode.getChildren("actions").get(0).getChildren();
		for (Element ele : actionList) {
			Attribute attribute = ele.getAttribute("url");
			List<Element> resultList = ele.getChildren();
			map.put(attribute.getValue(), resultList.get(0).getValue());
		}
		return map;
	}
	
	/**
	 * @description:读取xml文件  
	 * @param: path 文件路径
	 * @author:wuhn
	 * @createDate:2018年4月25日 上午10:21:37
	 */
	public  static Map<String,String> readXML(String path){
		if(StringUtils.isBlank(path)){
			path="src/main/resources/jeecgMVC.xml";
		}
		try {
			document = builder.build(new File(path));
		} catch (JDOMException | IOException e) {
			e.printStackTrace();
		}
		return parseXML();
	} 
	
	/**
	 * @description:读取xml文件 
	 * @param: file  文件路径对象
	 * @author:wuhn
	 * @createDate:2018年4月25日 上午10:22:27
	 */
	public static Map<String,String> readXML(File file){
		file=new File("src/main/resources/jeecgMVC.xml");
		try {
			document = builder.build(file);
		} catch (JDOMException | IOException e) {
			e.printStackTrace();
		}
		return parseXML();
	}
}
